home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xprof / xprof / message.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  174 lines

  1. /*==================================================================
  2.  *      File :          message.c
  3.  *      Package:        Xprof
  4.  *
  5.  *      Author :        Aloke Gupta.
  6.  *
  7.  *  (C) Copyright 1992, Aloke Gupta.
  8.  *==================================================================*/
  9.  
  10. /*      
  11.  *    1. InitMsgStats   (MsgStats *mstats, long timestamp, Detailed detailed,
  12.  *                Grain size_grain)
  13.  *    2. FillMsgStats   (Msgstats *mstats, long timestamp, long size,
  14.                 long bytes)
  15.  *    3. PrintMsgStats  (FILE *fp, MsgStats *mstats, char *message)
  16.  *    4. PrintMsgDetails(FILE *fp, MsgStats *mstats, char *message)
  17.  */
  18. #include <stdio.h>
  19. #include <memory.h>
  20. #include "common.h"
  21.  
  22. int MAXBUCKETS_SIZE=4096;    /* Array size for size stats */
  23.  
  24. /* Initialize the MsgStats structure */
  25. InitMsgStats(mstats, timestamp, detailed, size_grain)
  26. MsgStats *mstats;
  27. long     timestamp;
  28. Detailed detailed;        /* Should we maintain detailed information ? */
  29. Grain     size_grain;
  30. {
  31.     mstats->invoked     = TRUE;
  32.     mstats->number      = 0;
  33.     mstats->total_bytes = 0;
  34.     mstats->last_time   = timestamp;
  35.     mstats->size_grain  = size_grain;
  36.  
  37.     if (detailed == DETAILED) {
  38.     mstats->detailed    = DETAILED;
  39.  
  40.     mstats->iat_distbn  = (long *) calloc(MAXBUCKETS_IAT, sizeof(long));
  41.     mstats->max_iat     = 0;
  42.     mstats->min_iat     = MAXLONG;
  43.  
  44.     mstats->size_distbn = (long *) calloc(MAXBUCKETS_SIZE, sizeof(long));
  45.     mstats->max_size    = 0;
  46.     mstats->min_size    = MAXLONG;
  47.     if ((mstats->iat_distbn == NULL) || (mstats->size_distbn ==NULL)) {
  48.         fprintf(stderr, "Error: Memory allocation failed\n");
  49.         exit(1);
  50.     }
  51.     }
  52. }
  53.  
  54. /*     FillMsgStats.
  55.  * The timestamp, bytes, and size parameters control the filling of various
  56.  * fields in the MsgStats structure. Pass a -1 in these to suppress this.
  57.  *     timestamp: max_iat,  min_iat,  iat_distbn, last_time
  58.  *    size:       max_size, min_size, size_distbn
  59.  *    bytes:       number,   total_bytes
  60.  */
  61. FillMsgStats(mstats, timestamp, size, bytes)
  62. MsgStats *mstats;
  63. long timestamp;
  64. long size;
  65. long bytes;
  66. {
  67.     long bucket;
  68.  
  69.     /*
  70.     if ((mstats->detailed == TERSE) && ((timestamp > 0) || (size > 0)) ){
  71.     fprintf(stderr,"Error: Attempt to Fill uninitialized structure !!\n");
  72.     return(1);
  73.     }
  74.     */
  75.  
  76.     /* Fill the interarrival time distribution */
  77.     if (mstats->detailed == DETAILED) {
  78.       if (timestamp >= 0) {
  79.     bucket = (timestamp - mstats->last_time) / (long) IAT_GRAIN;
  80.     if (bucket > mstats->max_iat)
  81.         mstats->max_iat = bucket;
  82.     if (bucket < mstats->min_iat)
  83.         mstats->min_iat = bucket;
  84.  
  85.     /* Actual array is filled to a log2 scale */
  86.     if (bucket > 0)
  87.         bucket = (long ) mylog2(bucket);
  88.  
  89.     if (MAXBUCKETS_IAT <= bucket)
  90.         bucket = MAXBUCKETS_IAT - 1;
  91.     mstats->iat_distbn[bucket] ++;
  92.  
  93.     /* Update the timestamp */
  94.     mstats->last_time = timestamp;
  95.       }
  96.  
  97.       /*
  98.        * Fill in the size distribution for the requests.
  99.        */
  100.       if (size >= 0) {
  101.     bucket = size / (long) mstats->size_grain;
  102.     if (bucket > mstats->max_size)
  103.         mstats->max_size = bucket;
  104.     if (bucket < mstats->min_size)
  105.         mstats->min_size = bucket;
  106.     if (MAXBUCKETS_SIZE <= bucket)
  107.         bucket = MAXBUCKETS_SIZE - 1;
  108.     mstats->size_distbn[bucket] ++;
  109.       }
  110.     }
  111.  
  112.     if (bytes >= 0) {
  113.     mstats->number ++;
  114.     mstats->total_bytes += bytes;
  115.     }
  116. }
  117.  
  118. PrintMsgStats(fp, mstats, message)
  119. FILE *fp;
  120. MsgStats *mstats;    /* Data structure to print out */
  121. char     *message;    /* Header message */
  122. {
  123.     fprintf(fp, "\n\t\t***** Statistics for %s *****\n", message);
  124.  
  125.     if (mstats->invoked == FALSE) {
  126.     return;
  127.     }
  128.     if (mstats->number > 0)
  129.       if (mstats->detailed == DETAILED) {/* Detailed stats were maintained */
  130.     fprintf(fp,"Inter-arrival time distribution (ms):\n");
  131.     process_stats(fp,mstats->min_iat, mstats->max_iat, mstats->iat_distbn,
  132.                 IAT_GRAIN, (long) MAXBUCKETS_IAT, LOG2);
  133.  
  134.     fprintf(fp,"Size distribution:\n");
  135.     process_stats(fp,mstats->min_size,mstats->max_size, mstats->size_distbn,
  136.                 mstats->size_grain, (long) MAXBUCKETS_SIZE, LINEAR);
  137.       }
  138. }
  139.  
  140. PrintMsgDetails(fp, mstats, message)
  141. FILE *fp;
  142. MsgStats *mstats;    /* Data structure to print out */
  143. char     *message;    /* Header message */
  144. {
  145.     long i;
  146.  
  147.     if (mstats->invoked == FALSE) {
  148.     fprintf(fp,"Error: This structure has never been invoked\n");
  149.     exit(1);
  150.     }
  151.     fprintf(fp,"\n\t\t***** Raw Statistics for %s *****\n", message);
  152.  
  153.     if (mstats->number > 0)
  154.       if (mstats->detailed == DETAILED) {/* Detailed stats were maintained */
  155.     fprintf(fp,"\tInter-arrival time distribution (ms):\n");
  156.     fprintf(fp,"%7s %10s\n","i", "freq");
  157.     for (i = 0; i < MAXBUCKETS_IAT; i++) {
  158.         if (mstats->iat_distbn[i] > 0)
  159.         fprintf(fp,"%7ld %10ld\n",(long) myexp2(i) * (long) IAT_GRAIN,
  160.             mstats->iat_distbn[i]);
  161.     }
  162.  
  163.     fprintf(fp,"\tSize distribution:\n");
  164.     fprintf(fp,"%7s %10s\n","i", "freq");
  165.  
  166.     for (i = 0; i < MAXBUCKETS_SIZE; i++) {
  167.         if (mstats->size_distbn[i] > 0)
  168.         fprintf(fp,"%7ld %10ld\n", (long) i * (long) mstats->size_grain,
  169.             mstats->size_distbn[i]);
  170.     }
  171.  
  172.       }
  173. }
  174.